package mi.payment.utils;

import android.app.Application;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import mi.payment.BuildConfig;
import mi.payment.MyApplication;
import sunmi.sunmiui.utils.LogUtil;

/**
 * @author Administrator on 2016/11/17.
 */
public class Config {
    public static final String TAG = Config.class.getName();
    public static final boolean D = true;
    public static String mPackagePath = "/data/data/"+ MyApplication.getInstance().getPackageName();
    public final static String mConfigPath = "Config";
    public final static String mConfigFile = "appinfo.cfg";

    public final static String mSecurePath = "Secure";
    public final static String mAIDFile = "aids.dat";
    public final static String mRIDFile = "rids.dat";

    public final static String PATH_SECURE = mPackagePath + "/" + Config.mSecurePath;
    public final static String PATH_AID_FILE = mPackagePath + "/" + Config.mSecurePath + "/" + Config.mAIDFile;
    public final static String PATH_CAPK_FILE = mPackagePath + "/" + Config.mSecurePath + "/" + Config.mRIDFile;

    public final static String MERCHANT_ID = "Merchant_Id";
    public final static String TERMINAL_ID = "Terminal_Id";

    public static void clean() {
        File file = new File(PATH_SECURE);
        deleteDir(file);
    }

    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
    public static boolean init() {
        boolean flag = false;
        boolean mock = TextUtils.equals(BuildConfig.FLAVOR, "UnionPay");
        try {
            File path = new File(PATH_SECURE);
            if (!path.exists() || !path.isDirectory()) {
                path.mkdir();
            }
            flag = path.exists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogUtil.e(TAG, "Config init:" + flag);
        if (mock) {
            mockData();
        }
        return flag;
    }

    private static void mockData() {
        Map<String, String> aidMap = new HashMap<>();
        aidMap.put("9F0608A000000333010101", "9F0608A000000333010101DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF2106100000000000");
        aidMap.put("9F0608A000000333010102", "9F0608A000000333010102DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF2106100000000000");
        aidMap.put("9F0608A000000333010103", "9F0608A000000333010103DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF2106100000000000");
        aidMap.put("9F0608A000000333010106", "9F0608A000000333010106DF0101009F08020020DF1105D84000A800DF1205D84004F800DF130500100000009F1B040000C350DF150400000000DF160199DF170199DF14039F3704DF1801019F7B06100000000000DF1906100000000000DF2006100000000000DF2106100000000000");
        aidMap.put("9F0608A000000333010106", "9F0608A0000003330101069F080200209F1B04000027109F7B06000000080000DF010101DF1105FCF0E40800DF1205FCF8E4F880DF13050010000000DF14039F3704DF150400000000DF160120DF170110DF180101DF1906000000050000DF2006000000100000DF2106000000010000");
        aidMap.put("9F0608A000000333010101", "9F0608A0000003330101029F080200209F1B04000027109F7B06000000080000DF010101DF1105FCF0E40800DF1205FCF8E4F880DF13050050000000DF14039F3704DF150400000000DF160100DF170100DF180101DF1906000000050000DF2006000000100000DF2106000000010000");
        aidMap.put("9F0608A000000333010102", "9F0608A0000003330101029F080200209F1B04000027109F7B06000000080000DF010101DF1105FCF0E40800DF1205FCF8E4F880DF13050050000000DF14039F3704DF150400000000DF160100DF170100DF180101DF1906000000050000DF2006000000100000DF2106000000010000");
        aidMap.put("9F0608A000000333010103", "9F0608A0000003330101039F080200209F1B04000027109F7B06000000080000DF010101DF1105FCF0E40800DF1205FCF8E4F880DF13050010000000DF14039F3704DF150400000000DF160100DF170100DF180101DF1906000000050000DF2006000000100000DF2106000000010000");
        Properties propAID = new Properties();
        for (String aidTitle : aidMap.keySet()) {
            propAID.setProperty(aidTitle, aidMap.get(aidTitle));
        }
        Config.saveAIDs(propAID);

        Map<String, String> ridMap = new HashMap<>();
        ridMap.put("9F0605A0000003339F220103", "9F0605A0000003339F220103DF05083230323431323331DF060101DF070101DF0281B0B0627DEE87864F9C18C13B9A1F025448BF13C58380C91F4CEBA9F9BCB214FF8414E9B59D6ABA10F941C7331768F47B2127907D857FA39AAF8CE02045DD01619D689EE731C551159BE7EB2D51A372FF56B556E5CB2FDE36E23073A44CA215D6C26CA68847B388E39520E0026E62294B557D6470440CA0AEFC9438C923AEC9B2098D6D3A1AF5E8B1DE36F4B53040109D89B77CAFAF70C26C601ABDF59EEC0FDC8A99089140CD2E817E335175B03B7AA33DDF040103DF031487F0CD7C0E86F38F89A66F8C47071A8B88586F26");
        ridMap.put("9F0605A0000000039F220108", "9F0605A0000000039F220108DF0281B0D9FD6ED75D51D0E30664BD157023EAA1FFA871E4DA65672B863D255E81E137A51DE4F72BCC9E44ACE12127F87E263D3AF9DD9CF35CA4A7B01E907000BA85D24954C2FCA3074825DDD4C0C8F186CB020F683E02F2DEAD3969133F06F7845166ACEB57CA0FC2603445469811D293BFEFBAFAB57631B3DD91E796BF850A25012F1AE38F05AA5C4D6D03B1DC2E568612785938BBC9B3CD3A910C1DA55A5A9218ACE0F7A21287752682F15832A678D6E1ED0BDF031420D213126955DE205ADC2FD2822BD22DE21CF9A8DF040103DF05083230313431323331DF060101DF070101");
        ridMap.put("9F0605A0000000659F220114", "9F0605A0000000659F220114DF0281F8AEED55B9EE00E1ECEB045F61D2DA9A66AB637B43FB5CDBDB22A2FBB25BE061E937E38244EE5132F530144A3F268907D8FD648863F5A96FED7E42089E93457ADC0E1BC89C58A0DB72675FBC47FEE9FF33C16ADE6D341936B06B6A6F5EF6F66A4EDD981DF75DA8399C3053F430ECA342437C23AF423A211AC9F58EAF09B0F837DE9D86C7109DB1646561AA5AF0289AF5514AC64BC2D9D36A179BB8A7971E2BFA03A9E4B847FD3D63524D43A0E8003547B94A8A75E519DF3177D0A60BC0B4BAB1EA59A2CBB4D2D62354E926E9C7D3BE4181E81BA60F8285A896D17DA8C3242481B6C405769A39D547C74ED9FF95A70A796046B5EFF36682DC29DF0314C0D15F6CD957E491DB56DCDD1CA87A03EBE06B7BDF040103DF05083230313631323331DF060101DF070101");
        ridMap.put("9F0605A0000000039F220109", "9F0605A0000000039F220109DF0281F89D912248DE0A4E39C1A7DDE3F6D2588992C1A4095AFBD1824D1BA74847F2BC4926D2EFD904B4B54954CD189A54C5D1179654F8F9B0D2AB5F0357EB642FEDA95D3912C6576945FAB897E7062CAA44A4AA06B8FE6E3DBA18AF6AE3738E30429EE9BE03427C9D64F695FA8CAB4BFE376853EA34AD1D76BFCAD15908C077FFE6DC5521ECEF5D278A96E26F57359FFAEDA19434B937F1AD999DC5C41EB11935B44C18100E857F431A4A5A6BB65114F174C2D7B59FDF237D6BB1DD0916E644D709DED56481477C75D95CDD68254615F7740EC07F330AC5D67BCD75BF23D28A140826C026DBDE971A37CD3EF9B8DF644AC385010501EFC6509D7A41DF03141FF80A40173F52D7D27E0F26A146A1C8CCB29046DF040103DF05083230313631323331DF060101DF070101");
        ridMap.put("9F0605A0000000659F220109", "9F0605A0000000659F220109DF028180B72A8FEF5B27F2B550398FDCC256F714BAD497FF56094B7408328CB626AA6F0E6A9DF8388EB9887BC930170BCC1213E90FC070D52C8DCD0FF9E10FAD36801FE93FC998A721705091F18BC7C98241CADC15A2B9DA7FB963142C0AB640D5D0135E77EBAE95AF1B4FEFADCF9C012366BDDA0455C1564A68810D7127676D493890BDDF03144410C6D51C2F83ADFD92528FA6E38A32DF048D0ADF040103DF05083230303931323331DF060101DF070101");
        ridMap.put("9F0605A0000003339F220101", "9F0605A0000003339F220101DF028180BBE9066D2517511D239C7BFA77884144AE20C7372F515147E8CE6537C54C0A6A4D45F8CA4D290870CDA59F1344EF71D17D3F35D92F3F06778D0D511EC2A7DC4FFEADF4FB1253CE37A7B2B5A3741227BEF72524DA7A2B7B1CB426BEE27BC513B0CB11AB99BC1BC61DF5AC6CC4D831D0848788CD74F6D543AD37C5A2B4C5D5A93BDF0314E881E390675D44C2DD81234DCE29C3F5AB2297A0DF040103DF05083230303931323331DF060101DF070101");
        ridMap.put("9F0605A0000000659F220112", "9F0605A0000000659F220112DF0281B0ADF05CD4C5B490B087C3467B0F3043750438848461288BFEFD6198DD576DC3AD7A7CFA07DBA128C247A8EAB30DC3A30B02FCD7F1C8167965463626FEFF8AB1AA61A4B9AEF09EE12B009842A1ABA01ADB4A2B170668781EC92B60F605FD12B2B2A6F1FE734BE510F60DC5D189E401451B62B4E06851EC20EBFF4522AACC2E9CDC89BC5D8CDE5D633CFD77220FF6BBD4A9B441473CC3C6FEFC8D13E57C3DE97E1269FA19F655215B23563ED1D1860D8681DF0314874B379B7F607DC1CAF87A19E400B6A9E25163E8DF040103DF05083230313431323331DF060101DF070101");
        ridMap.put("9F0605A0000000039F220107", "9F0605A0000000039F220107DF028190A89F25A56FA6DA258C8CA8B40427D927B4A1EB4D7EA326BBB12F97DED70AE5E4480FC9C5E8A972177110A1CC318D06D2F8F5C4844AC5FA79A4DC470BB11ED635699C17081B90F1B984F12E92C1C529276D8AF8EC7F28492097D8CD5BECEA16FE4088F6CFAB4A1B42328A1B996F9278B0B7E3311CA5EF856C2F888474B83612A82E4E00D0CD4069A6783140433D50725FDF0314B4BC56CC4E88324932CBC643D6898F6FE593B172DF040103DF05083230313231323331DF060101DF070101");
        ridMap.put("9F0605A0000003339F220103", "9F0605A0000003339F220103DF0281B0B0627DEE87864F9C18C13B9A1F025448BF13C58380C91F4CEBA9F9BCB214FF8414E9B59D6ABA10F941C7331768F47B2127907D857FA39AAF8CE02045DD01619D689EE731C551159BE7EB2D51A372FF56B556E5CB2FDE36E23073A44CA215D6C26CA68847B388E39520E0026E62294B557D6470440CA0AEFC9438C923AEC9B2098D6D3A1AF5E8B1DE36F4B53040109D89B77CAFAF70C26C601ABDF59EEC0FDC8A99089140CD2E817E335175B03B7AA33DDF031487F0CD7C0E86F38F89A66F8C47071A8B88586F26DF040103DF05083230323431323331DF060101DF070101");
        ridMap.put("9F0605A0000000049F220103", "9F0605A0000000049F220103DF028180C2490747FE17EB0584C88D47B1602704150ADC88C5B998BD59CE043EDEBF0FFEE3093AC7956AD3B6AD4554C6DE19A178D6DA295BE15D5220645E3C8131666FA4BE5B84FE131EA44B039307638B9E74A8C42564F892A64DF1CB15712B736E3374F1BBB6819371602D8970E97B900793C7C2A89A4A1649A59BE680574DD0B60145DF03145ADDF21D09278661141179CBEFF272EA384B13BBDF040103DF05083230303931323331DF060101DF070101");
        ridMap.put("9F0605A0000000039F220101", "9F0605A0000000039F220101DF028180C696034213D7D8546984579D1D0F0EA519CFF8DEFFC429354CF3A871A6F7183F1228DA5C7470C055387100CB935A712C4E2864DF5D64BA93FE7E63E71F25B1E5F5298575EBE1C63AA617706917911DC2A75AC28B251C7EF40F2365912490B939BCA2124A30A28F54402C34AECA331AB67E1E79B285DD5771B5D9FF79EA630B75DF0314D34A6A776011C7E7CE3AEC5F03AD2F8CFC5503CCDF040103DF05083230303931323331DF060101DF070101");
        ridMap.put("9F0605A0000000049F220106", "9F0605A0000000049F220106DF0281F8CB26FC830B43785B2BCE37C81ED334622F9622F4C89AAE641046B2353433883F307FB7C974162DA72F7A4EC75D9D657336865B8D3023D3D645667625C9A07A6B7A137CF0C64198AE38FC238006FB2603F41F4F3BB9DA1347270F2F5D8C606E420958C5F7D50A71DE30142F70DE468889B5E3A08695B938A50FC980393A9CBCE44AD2D64F630BB33AD3F5F5FD495D31F37818C1D94071342E07F1BEC2194F6035BA5DED3936500EB82DFDA6E8AFB655B1EF3D0D7EBF86B66DD9F29F6B1D324FE8B26CE38AB2013DD13F611E7A594D675C4432350EA244CC34F3873CBA06592987A1D7E852ADC22EF5A2EE28132031E48F74037E3B34AB747FDF0314F910A1504D5FFB793D94F3B500765E1ABCAD72D9DF040103DF05083230323331323331DF060101DF070101");
        ridMap.put("9F0605A0000000659F220110", "9F0605A0000000659F220110DF02819099B63464EE0B4957E4FD23BF923D12B61469B8FFF8814346B2ED6A780F8988EA9CF0433BC1E655F05EFA66D0C98098F25B659D7A25B8478A36E489760D071F54CDF7416948ED733D816349DA2AADDA227EE45936203CBF628CD033AABA5E5A6E4AE37FBACB4611B4113ED427529C636F6C3304F8ABDD6D9AD660516AE87F7F2DDF1D2FA44C164727E56BBC9BA23C0285DF0314C75E5210CBE6E8F0594A0F1911B07418CADB5BABDF040103DF05083230313231323331DF060101DF070101");
        ridMap.put("9F0605A0000000049F220104", "9F0605A0000000049F220104DF028190A6DA428387A502D7DDFB7A74D3F412BE762627197B25435B7A81716A700157DDD06F7CC99D6CA28C2470527E2C03616B9C59217357C2674F583B3BA5C7DCF2838692D023E3562420B4615C439CA97C44DC9A249CFCE7B3BFB22F68228C3AF13329AA4A613CF8DD853502373D62E49AB256D2BC17120E54AEDCED6D96A4287ACC5C04677D4A5A320DB8BEE2F775E5FEC5DF0314381A035DA58B482EE2AF75F4C3F2CA469BA4AA6CDF040103DF05083230313731323331DF060101DF070101");
        ridMap.put("9F0605A0000003339F220102", "9F0605A0000003339F220102DF028190A3767ABD1B6AA69D7F3FBF28C092DE9ED1E658BA5F0909AF7A1CCD907373B7210FDEB16287BA8E78E1529F443976FD27F991EC67D95E5F4E96B127CAB2396A94D6E45CDA44CA4C4867570D6B07542F8D4BF9FF97975DB9891515E66F525D2B3CBEB6D662BFB6C3F338E93B02142BFC44173A3764C56AADD202075B26DC2F9F7D7AE74BD7D00FD05EE430032663D27A57DF031403BB335A8549A03B87AB089D006F60852E4B8060DF040103DF05083230323131323331DF060101DF070101");
        ridMap.put("9F0605A0000000049F220105", "9F0605A0000000049F220105DF0281B0B8048ABC30C90D976336543E3FD7091C8FE4800DF820ED55E7E94813ED00555B573FECA3D84AF6131A651D66CFF4284FB13B635EDD0EE40176D8BF04B7FD1C7BACF9AC7327DFAA8AA72D10DB3B8E70B2DDD811CB4196525EA386ACC33C0D9D4575916469C4E4F53E8E1C912CC618CB22DDE7C3568E90022E6BBA770202E4522A2DD623D180E215BD1D1507FE3DC90CA310D27B3EFCCD8F83DE3052CAD1E48938C68D095AAC91B5F37E28BB49EC7ED597DF0314EBFA0D5D06D8CE702DA3EAE890701D45E274C845DF040103DF05083230323331323331DF060101DF070101");
        ridMap.put("9F0605A0000003339F220104", "9F0605A0000003339F220104DF0281F8BC853E6B5365E89E7EE9317C94B02D0ABB0DBD91C05A224A2554AA29ED9FCB9D86EB9CCBB322A57811F86188AAC7351C72BD9EF196C5A01ACEF7A4EB0D2AD63D9E6AC2E7836547CB1595C68BCBAFD0F6728760F3A7CA7B97301B7E0220184EFC4F653008D93CE098C0D93B45201096D1ADFF4CF1F9FC02AF759DA27CD6DFD6D789B099F16F378B6100334E63F3D35F3251A5EC78693731F5233519CDB380F5AB8C0F02728E91D469ABD0EAE0D93B1CC66CE127B29C7D77441A49D09FCA5D6D9762FC74C31BB506C8BAE3C79AD6C2578775B95956B5370D1D0519E37906B384736233251E8F09AD79DFBE2C6ABFADAC8E4D8624318C27DAF1DF0314F527081CF371DD7E1FD4FA414A665036E0F5E6E5DF040103DF05083230323531323331DF060101DF070101");

        Properties propRID = new Properties();
        for (String ridTitle : ridMap.keySet()) {
            propRID.setProperty(ridTitle, ridMap.get(ridTitle));
        }
        Config.saveRIDs(propRID);
    }

    public static boolean loadAppConfig(String appPath) {
        Properties prop = loadConfiguration(appPath);
        return true;
    }

    public static Properties loadConfiguration(String appPath) {
        String cfgFile = appPath + "/" + Config.mConfigPath + "/" + Config.mConfigFile;
        Properties properties = new Properties();
        FileInputStream is = null;
        try {
            is = new FileInputStream(cfgFile);
            properties.load(is);
            is.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return properties;
    }

    public static void saveConfiguration(String appPath, Properties properties) {
        FileOutputStream s = null;
        try {
            String cfgPath = appPath + "/" + Config.mConfigPath;
            File path = new File(cfgPath);
            if (!path.exists() || !path.isDirectory()) {
                path.mkdir();
            }
            String cfgFile = cfgPath + "/" + Config.mConfigFile;

            s = new FileOutputStream(cfgFile, false);
            properties.store(s, "");
            s.flush();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Properties loadAIDs() {
        String secFile = PATH_AID_FILE;
        Properties properties = new Properties();
        FileInputStream is = null;
        try {
            is = new FileInputStream(secFile);
            properties.load(is);
            is.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return properties;
    }

    public static void saveAIDs(Properties properties) {
        FileOutputStream s = null;
        try {
            String secPath = PATH_SECURE;
            File path = new File(secPath);
            if (!path.exists() || !path.isDirectory()) {
                path.mkdir();
            }
            String secFile = PATH_AID_FILE;

            s = new FileOutputStream(secFile, false);
            properties.store(s, "");
            s.flush();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Properties loadRIDs() {
        String secFile = PATH_CAPK_FILE;
        Properties properties = new Properties();
        FileInputStream is = null;
        try {
            is = new FileInputStream(secFile);
            properties.load(is);
            is.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return properties;
    }

    public static void saveRIDs(Properties properties) {
        FileOutputStream s = null;
        try {
            String secPath = PATH_SECURE;
            File path = new File(secPath);
            if (!path.exists() || !path.isDirectory()) {
                path.mkdir();
            }
            String secFile = PATH_CAPK_FILE;

            s = new FileOutputStream(secFile, false);
            properties.store(s, "");
            s.flush();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
